﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using Pixysoft.Framework.Configurations;

namespace Pixysoft.Framework.Logs.Core
{
    class RecordCollection : IRecordCollection
    {
        IXmlNodesCollection collection = null;

        public RecordCollection(IXmlNodesCollection collection)
        {
            this.collection = collection;
        }

        public void Sort()
        {
            List<IRecord> list = new List<IRecord>();

            foreach (IRecord rec in this)
            {
                list.Add(rec);
            }

            list.Sort();

            this.Clear();

            foreach (IRecord rec in list)
            {
                this.Add(rec);
            }
        }

        public IRecord Add(string elementName)
        {
            IXmlNode node = collection.Add(elementName);

            Record record = new Record(node);
            record.Id = StringHelper.GetId();
            record.RawDate = StringHelper.GetRawDateTime();

            return record;
        }

        public IRecord Add(IXmlNode node)
        {
            IXmlNode addnode = collection.Add(node.ElementName);

            Record addrecord = new Record(addnode);
            //addrecord.Id = node.Properties[StringHelper.default_attribute_id];
            //addrecord.RawDate = node.Properties[StringHelper.default_attribute_time];
            addrecord.Text = node.Text;

            foreach (IXmlProperty property in node.Properties)
            {
                addrecord.Properties.Add(property.Key, property.Value);
            }

            foreach (IXmlNode subnode in node.Nodes)
            {
                addrecord.Records.Add(subnode);
            }

            return addrecord;
        }

        public void Add(IRecord record)
        {
            IXmlNode node = collection.Add(record.ElementName);

            Record addrecord = new Record(node);
            //addrecord.Id = record.Id;
            //addrecord.RawDate = record.RawDate;
            addrecord.Text = record.Text;

            foreach (IRecordProperty property in record.Properties)
            {
                addrecord.Properties.Add(property.Key, property.Value);
            }

            foreach (IRecord subrecord in record.Records)
            {
                addrecord.Records.Add(subrecord);
            }
        }

        public void Clear()
        {
            collection.Clear();
        }

        public bool Contains(IRecord item)
        {
            return collection.Contains(item.ElementName);
        }

        public int Count
        {
            get { return collection.Count; }
        }

        public IRecord this[int index]
        {
            get
            {
                IXmlNode node = collection[index];

                if (node == null)
                    return null;

                return new Record(node);
            }
        }

        public bool Contains(string elementName)
        {
            return collection.Contains(elementName);
        }


        #region IEnumerable<IRecord> 成员

        public IEnumerator<IRecord> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerator<IRecord> 成员

        public IRecord Current
        {
            get { return new Record(collection.Current); }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            collection.Dispose();
        }

        #endregion

        #region IEnumerator 成员

        object System.Collections.IEnumerator.Current
        {
            get { return new Record(collection.Current); }
        }

        public bool MoveNext()
        {
            return collection.MoveNext();
        }

        public void Reset()
        {
            collection.Reset();
        }

        #endregion

    }
}
